package com.yami.shop.seckill.common.listener;


import com.yami.shop.bean.enums.OrderType;
import com.yami.shop.bean.event.CancelOrderEvent;
import com.yami.shop.bean.order.CancelOrderOrder;
import com.yami.shop.seckill.common.service.SeckillOrderService;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 取消订单事件
 * @author yami
 */
@Component("seckillCancelOrderListener")
@AllArgsConstructor
public class CancelOrderListener {


    private final SeckillOrderService seckillOrderService;

    @EventListener(CancelOrderEvent.class)
    @Order(CancelOrderOrder.SECKILL)
    public void seckillCancelOrderEventListener(CancelOrderEvent event) {
        String orderNumber = event.getOrder().getOrderNumber();
        // 不是秒杀订单直接返回
        if (!OrderType.SECKILL.value().equals(event.getOrder().getOrderType())) {
            return;
        }

        seckillOrderService.cancelUnpayOrderByOrderNumber(orderNumber);
    }

}
